package com.hs.dao.impl;

import com.hs.dao.TeacherDao;
import com.hs.pojo.Teacher;
import com.hs.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {
    @Override
    public List<Teacher> selectTeacher(int startIndex, int pageSize) {
        String sql="select * from teacher limit ?,?";
        Object obj[]={startIndex,pageSize};
        ResultSet res = JDBCUtils.query(sql, obj);
        List<Teacher> list=new ArrayList<>();
        while (true){
            try {
                if (!res.next())break;
                Teacher teacher=new Teacher(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getInt(8)


                );
                list.add(teacher);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JDBCUtils.close();
        return list;
    }

    @Override
    public int SelectCount() {
        String sql="select count(1) from teacher";
        Object obj[]={};
        ResultSet res = JDBCUtils.query(sql, obj);
        int count=0;
        while (true){
            try {
                if (!res.next())break;
                count=res.getInt(1);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JDBCUtils.close();
        return count;
    }

    @Override
    public int insertTeacher(Teacher teacher) {
        String sql="insert into teacher(name,img,phone,job,style,detail,did) Values(?,?,?,?,?,?,?)";
        Object obj[]={
                teacher.getName(),
                teacher.getImg(),
                teacher.getPhone(),
                teacher.getJob()
                ,teacher.getStyle(),
                teacher.getDetail(),
                teacher.getDid()
        };
        int insert = JDBCUtils.update(sql, obj);
        return insert;
    }

    @Override
    public Teacher selectTeacherById(int id) {
        String sql="select * from teacher where id=?";
        Object obj[]={id};
        ResultSet res = JDBCUtils.query(sql, obj);
        Teacher teacher=new Teacher();
        while (true){
            try {
                if (!res.next())break;
                teacher=new Teacher(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getInt(8)

                );
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JDBCUtils.close();
        return teacher;
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        String sql="update teacher set name=?,img=?,phone=? ,job=?,style=?,detail=?,did=? where id=?";
        Object obj[]={
                teacher.getName(),
                teacher.getImg(),
                teacher.getPhone(),
                teacher.getJob(),
                teacher.getStyle(),
                teacher.getDetail(),
                teacher.getDid(),
                teacher.getId()};
        int update = JDBCUtils.update(sql, obj);

        return update;
    }

    @Override
    public int deleteTeacher(int id) {
        String sql="delete from teacher where id=?";
        Object obj[]={id};
        int delete = JDBCUtils.update(sql, obj);
        return delete;
    }
}
